package com.cgwx.service.impl;

import com.cgwx.config.BusinessException;
import com.cgwx.entity.File;
import com.cgwx.entity.Folder;
import com.cgwx.mapper.FavoritesMapper;
import com.cgwx.mapper.FileMapper;
import com.cgwx.service.IFileService;
import com.cgwx.service.IFolderService;
import com.cgwx.service.IUtilService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Service
@Slf4j
public class FileServiceImpl implements IFileService {
    @Resource
    private FileMapper fileMapper;

    @Resource
    private FavoritesMapper favoritesMapper;

    @Resource
    private IUtilService utilService;

    @Resource
    private IFolderService folderService;
    @Override
    public List<File> getAllFile() {
        return fileMapper.getAllFile();
    }

    @Override
    public List<File> getFile(File file) {
        return fileMapper.getFile(file);
    }

    @Override
    public List<File> OpenPersonal(String account, String entityId) {
        File file = new File();
        file.setParentId(entityId);
        return fileMapper.getFile(file);
    }

    @Override
    public List<String> OpenPersonalFavorites(String entityId, String account) {
        File file = new File();
        file.setParentId(entityId);
        return favoritesMapper.getFileFavorites(file, account);
    }

    @Override
    public String deleteFileById(String entityId) {
        boolean result = fileMapper.deleteFileById(entityId);
        if(result) return "删除成功!";
        else return "删除失败!";
    }

    @Override
    public String updateFileById(File file) {
        file.setUpdateTime(Timestamp.valueOf(LocalDateTime.now()));
        boolean result = fileMapper.updateFileById(file);
        if(result) return "更新成功!";
        else return "更新失败!";
    }

    @Override
    public String insertFile(String account, String name, String entityId, String roomId, String parentId, String fileId, String fileName, String fileUrl, float fileSize) {
        File file = new File();
        Timestamp time = (Timestamp.valueOf(LocalDateTime.now()));
        file.setCreateTime(time);
        String[] str = fileName.split("\\.");
        if(str.length < 2){
            file.setEntityName(fileName);
            file.setType("unknown");
        }
        else if(str.length > 2) {
            fileName = "";
            for(int i = 0; i < str.length - 1; i++) fileName += str[i] + '.';
            file.setEntityName(fileName.substring(0, fileName.length() - 1));
            file.setType(str[str.length - 1]);
        }
        else{
            file.setEntityName(str[0]);
            file.setType(str[1]);
        }
        //标签
        Folder parentFolder = new Folder();
        parentFolder.setEntityId(parentId);
        parentFolder = folderService.getFolder(parentFolder).get(0);
        file.setTag(parentFolder.getTag());
        file.setIsOuter(parentFolder.getIsOuter());
        file.setEntityRoom(roomId);
        file.setParentId(parentId);
        file.setOwnerAccount(account);
        file.setOwnerName(name);
        file.setFileId(fileId);
        file.setFileUrl(fileUrl);
        file.setFileSize(fileSize);
        file.setEntityId(entityId);
        file.setDownloadTimes(0);
        file.setViewTimes(0);
        boolean result = fileMapper.insertFile(file);
        if(!result){
            log.error("新增失败!");
            throw new BusinessException("新增失败!");
        }
        if(!utilService.updateParentTime(file.getEntityId()).equals("修改时间修改成功!")){
            log.error("父文件夹修改时间修改失败!");
            throw new BusinessException("父文件夹修改时间修改失败!");
        }
        else return "新增成功!";
    }

    @Override
    public String generateEntityId() {
        String uuid = UUID.randomUUID().toString();
        return "file_" + uuid;
    }
}